package com.dwh.mq.queue;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.boot.autoconfigure.jms.JmsProperties.AcknowledgeMode;

/**
 * p2p模式下的生产者2
 *
 * @author dongwenhua
 * @version 2017年12月28日
 */
public class Consumer2 {
	private static final String url = "tcp://127.0.0.1:61616";
	private static final String queue_name = "queue-test";
	
	public static void main(String[] args) throws JMSException {
		//创建连接工厂
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
		
		//创建连接
		Connection connection = connectionFactory.createConnection();
		
		//打开连接
		connection.start();
		
		//创建回话
		Session session = connection.createSession(false, AcknowledgeMode.AUTO.getMode());//是否使用事务、消息确认机制
		
		//创建队列
		Destination destination = session.createQueue(queue_name);
		
		//创建消费者
		MessageConsumer consumer = session.createConsumer(destination);
		consumer.setMessageListener(new MessageListener() {
			
			@Override
			public void onMessage(Message msg) {
				TextMessage message = (TextMessage) msg;
				try {
					System.out.println(message.getText());
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		});
	}
}
